package copyengine.ui.infoTree
{
    import copyengine.infoTree.meta.CESDisplayObjectMeta;
    import copyengine.infoTree.meta.CESFileMeta;
    import copyengine.resource.CEResUtils;
    import copyengine.resource.resProxy.CEResProxy;
    import copyengine.ui.infoTree.buildTree.InfoTreeBuildResult;
    import copyengine.ui.infoTree.buildTree.InfoTreeBuilderBuildTreeHelper;
    import copyengine.ui.infoTree.loadTexture.ITTextureLoadHelper;
    import copyengine.ui.infoTree.loadTexture.InfoTreeBuilderLoadTextureHelper;
    import copyengine.utils.CECallbackFunction;

    public class InfoTreeBuildFacade
    {
        private static var mInstance:InfoTreeBuildFacade;

        public static function  get instance():InfoTreeBuildFacade {
            if (mInstance == null) {
                mInstance = new InfoTreeBuildFacade();
            }
            return mInstance;
        }

        private var buildHelper:InfoTreeBuilderBuildTreeHelper;

        public function InfoTreeBuildFacade() {
            buildHelper = new InfoTreeBuilderBuildTreeHelper();
            buildHelper.initialize();
        }

        //=================//
        //==  加载相关
        //=================//

        /**
         * TODO:: 删除,以后全部用loadAllNeedTextures函数
         *
         * 加载以*.ce结尾的InfoTree描述文件 以及该ce中所有会用到的纹理信息
         *
         * 加载内容有:
         * 1· 该描述文件 (*.ce)
         * 2· 该描述文件中所需要用到的资源
         *
         * 注意!! : 仅用于ShareTexture 非ShareTexture的方式 一下加载所有的资源没有意义
         */
        public function loadCEFileAndWillUseTexture(_fileMetaName:String, _resProxy:CEResProxy, _finishCallback:CECallbackFunction):void {
            var loadHelper:InfoTreeBuilderLoadTextureHelper = new InfoTreeBuilderLoadTextureHelper();
            loadHelper.loadCEFileAndWillUseTexture(_fileMetaName, _resProxy, _finishCallback);
            //挂CEMemoryHolder 自销毁
            loadHelper = null;
        }

        /**
         * 加载 $_fileName.ce文件
         * 如果使用ShareTexture方式则加载改CE文件中所有用到的纹理文件
         * 如果不是ShareTexture方式则加载$_symbolName该元件所用到的所有纹理
         *
         * 当为ShareTexture方式时候 $_symbolName = null 即可
         *
         * @param _fileName
         * @param _sysmbolName
         * @param _resProxy
         * @param _finishCallback
         */
        public function loadAllNeedTextures(_fileName:String, _symbolName:String, _resProxy:CEResProxy, _finishCallback:CECallbackFunction):void {
            new ITTextureLoadHelper(_fileName, _symbolName, _resProxy, _finishCallback);
        }


        //=================//
        //== 构建显示对象
        //=================//

        /**
         * 构建一个显示对象树
         *
         *  注意!! : 在构建显示对象之前需要保证,该显示对象所需要用到的所有纹理均已经加载完成
         *
         */
        public function buildDisplayObject(_mcMeta:CESDisplayObjectMeta, _resProxy:CEResProxy):InfoTreeBuildResult {
            return buildHelper.buildDisplayObject(_mcMeta, _resProxy);
        }

        //=====================
        //== DisplayObject
        //=====================

        /**
         * 根据文件名和元件名 返回BuildResult,
         *
         * 1`  传入的resProxy需要已经加载了需要Build目标的所有素材
         * 2`  传入的resProxy需要加载需要Build目标的描述文件 xxx.ce
         * 3`  fileName 会默认添加 .ce结尾 比如FLA名称叫GUI.fla  导出的描述文件会命名为GUI.ce  此时仅需要传入GUI做参数即可
         */
        public function buildDisplayObject2(_symbolName:String, _fileName:String, _resProxy:CEResProxy):InfoTreeBuildResult {
            var fileMeta:CESFileMeta = CEResUtils.getDecompressedObj(_fileName + ".ce", _resProxy);
            var mcMeta:CESDisplayObjectMeta = fileMeta.allSubSymbolDic[_symbolName];
            var buildResult:InfoTreeBuildResult = InfoTreeBuildFacade.instance.buildDisplayObject(mcMeta, _resProxy);
            return buildResult;
        }
    }
}
